package com.jp.knowledge.dialog;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.PopupWindow;

import com.jp.knowledge.R;
import com.jp.knowledge.adapter.skill.SkillDetailPopupWindowMainAdapter;
import com.jp.knowledge.adapter.skill.SkillDetailPopupWindowSubAdapter;
import com.jp.knowledge.model.PositionKnowlegeMode;
import com.jp.knowledge.model.SkillModel;
import com.jp.knowledge.my.base.RecycleVAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 528787067@qq.com on 2017/4/21.
 */

public class SkillDetailPopuWindow extends PopupWindow {

    private Context mContext;
    private View contentView;
    private RecyclerView mainRv;
    private RecyclerView subRv;

    private View anchor;
    private List<PositionKnowlegeMode.ListBeanX> data;
    private int mainPosition;
    private int subPosition;

    private SkillDetailPopupWindowMainAdapter mainAdapter;
    private SkillDetailPopupWindowSubAdapter subAdapter;

    public SkillDetailPopuWindow(Context context, View anchor, List<PositionKnowlegeMode.ListBeanX> data, final int mainPosition, final int subPosition){
        mContext = context;
        this.anchor = anchor;
        this.data = data;
        this.mainPosition = mainPosition;
        this.subPosition = subPosition;
        contentView = View.inflate(mContext, R.layout.skill_popuwindow_view, null);
        mainRv = (RecyclerView) contentView.findViewById(R.id.skill_detail_popuwindow_main_rv);
        subRv = (RecyclerView) contentView.findViewById(R.id.skill_detail_popuwindow_sub_rv);
        mainRv.getLayoutParams().width = (int) (mContext.getResources().getDisplayMetrics().widthPixels * 0.3);

        setWidth(mContext.getResources().getDisplayMetrics().widthPixels);
        setHeight((int) (mContext.getResources().getDisplayMetrics().heightPixels * 0.4));
        setBackgroundDrawable(new ColorDrawable(0x00000000));
        setFocusable(true);
        setContentView(contentView);
        setAnimationStyle(R.style.skill_popuwindow_anim);

        mainRv.setHasFixedSize(true);
        mainRv.setLayoutManager(new LinearLayoutManager(mContext));
        mainAdapter = new SkillDetailPopupWindowMainAdapter(mContext, data, mainPosition);
        mainRv.setAdapter(mainAdapter);

        subRv.setHasFixedSize(true);
        subRv.setLayoutManager(new LinearLayoutManager(mContext));
        try {
            subAdapter = new SkillDetailPopupWindowSubAdapter(mContext, data.get(mainPosition).getList(), subPosition);
        } catch (Exception e){
            subAdapter = new SkillDetailPopupWindowSubAdapter(mContext, new ArrayList<PositionKnowlegeMode.ListBeanX.ListBean>(), subPosition);
        }finally {
            subRv.setAdapter(subAdapter);
        }

        mainAdapter.addItemClickListener(new RecycleVAdapter.OnItemClickListener() {
            @Override
            public void itemSelect(int position) {
                if(mainAdapter.getPosition() == position)
                    return;
                mainAdapter.setPosition(position);
                mainAdapter.notifyDataSetChanged();
                if(SkillDetailPopuWindow.this.mainPosition == position) {
                    subAdapter.setPosition(SkillDetailPopuWindow.this.subPosition);
                    subRv.scrollToPosition(SkillDetailPopuWindow.this.subPosition);
                } else {
                    subAdapter.setPosition(-1);
                    subRv.scrollToPosition(0);
                }
                subAdapter.updateAdapter(SkillDetailPopuWindow.this.data.get(position).getList());
            }
        });
        subAdapter.addItemClickListener(new RecycleVAdapter.OnItemClickListener() {
            @Override
            public void itemSelect(int position) {
                if(subAdapter.getPosition() == position)
                    return;
                SkillDetailPopuWindow.this.mainPosition = mainAdapter.getPosition();
                SkillDetailPopuWindow.this.subPosition = position;
                subAdapter.setPosition(position);
                subAdapter.notifyDataSetChanged();
                dismiss();
                if(onItemSelectListener != null)
                    onItemSelectListener.onItemSelect(SkillDetailPopuWindow.this.mainPosition,
                            SkillDetailPopuWindow.this.subPosition);
            }
        });
    }

    public void show(){
        mainAdapter.setPosition(mainPosition);
        mainAdapter.notifyDataSetChanged();
        subAdapter.setPosition(subPosition);
        subAdapter.updateAdapter(data.get(mainPosition).getList());
        mainRv.scrollToPosition(mainPosition);
        subRv.scrollToPosition(subPosition);
        showAsDropDown(anchor);
    }

    private OnItemSelectListener onItemSelectListener;
    public void setOnItemSelectListener(OnItemSelectListener onItemSelectListener){
        this.onItemSelectListener = onItemSelectListener;
    }
    public interface OnItemSelectListener{
        void onItemSelect(int mainPosition, int subPosition);
    }
}
